package frame.laxcen.response;


import frame.laxcen.BaseFrame;
import frame.laxcen.dataobject.DataObject_0001_01;
import frame.laxcen.dataobject.DataObject_0001_12;
import frame.laxcen.exception.IllegalFrameFormatException;
import frame.laxcen.request.Request_0001_12;
import frame.laxcen.response.annotation.FrameFeature;
import frame.laxcen.util.FrameReader;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;

// 心跳包 自动处理 原样发回去
@FrameFeature(MessageType = 0b0001, Mid = 0x12)
public class Response_0001_12 extends BaseResponse {

    // 判断是否是心跳包
    public static final Predicate<BaseFrame> isHeartBeat = (frame) -> frame.CW.IsUpload && frame.CW.MessageType == 0b0001 && frame.CW.MID == 0x12;

    public Response_0001_12(BaseFrame baseFrame) throws IllegalFrameFormatException {

        super(baseFrame);

        getContentMap().putAll(explainToMap());
    }

    public Request_0001_12 reflectToRequest() {

        return new Request_0001_12(Integer.parseInt(getContentMap().getOrDefault("确认的消息序号", "0")));
    }

    @Override
    protected Map<String, String> analyzeResponse(FrameReader reader) throws IllegalFrameFormatException {

        Map<String, String> result = new HashMap<>();

        result.put("确认的消息序号", "" + reader.readU32());

        return result;
    }

    public DataObject_0001_12 getDataObject() {

        return new DataObject_0001_12(getContentMap());
    }
}
